package com.ymm.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ymm.domain.entity.Role;
import com.ymm.domain.vo.PageVo;
import com.ymm.domain.vo.ResponseResult;
import com.ymm.mapper.RoleMapper;
import com.ymm.service.RoleService;
import com.ymm.service.UserRoleService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author 椰咩咩咩
 * @since 2023-04-03 04:15:03
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private UserRoleService userRoleService;

    @Override
    public ResponseResult pageRoles(Integer pageNum, Integer pageSize, String roleName) {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(roleName), Role::getRoleName, roleName);
        wrapper.orderByAsc(Role::getRoleSort);
        Page<Role> page = new Page<>(pageNum, pageSize);
        page(page, wrapper);
        return ResponseResult.okResult(new PageVo(page.getRecords(), page.getTotal()));
    }

    @Override
    public ResponseResult userNotHasRoles(Long id) {
        List<Long> ids = userRoleService.getRIdByUserId(id);
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.notIn(ids.size() > 0, Role::getId, ids);
        List<Role> roles = list(wrapper);
        return ResponseResult.okResult(roles);
    }

    @Override
    public ResponseResult userHasRoles(Long id) {
        List<Long> ids = userRoleService.getRIdByUserId(id);
        if (ids.size() <= 0) {
            return ResponseResult.okResult(new ArrayList<>());
        }
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Role::getId, ids);
        List<Role> roles = list(wrapper);
        return ResponseResult.okResult(roles);
    }

}

